package com.industrial.premu.controller;

import java.security.Security;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.security.utils.SecurityUtils;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuRate;
import com.industrial.premu.service.IPreMuRateService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 评级管理Controller
 * 
 * @author Zhou_S
 * @date 2024-01-22
 */
@RestController
@RequestMapping("/rate")
public class PreMuRateController extends BaseController
{
    @Autowired
    private IPreMuRateService preMuRateService;

    /**
     * 查询评级管理列表
     */
    @RequiresPermissions("premu:rate:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuRate preMuRate)
    {
        startPage();
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        preMuRate.setDistrictCode(district);
        List<PreMuRate> list = preMuRateService.selectPreMuRateList(preMuRate);
        return getDataTable(list);
    }

    /**
     * 导出评级管理列表
     */
    @RequiresPermissions("premu:rate:export")
    @Log(title = "评级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuRate preMuRate)
    {
        List<PreMuRate> list = preMuRateService.selectPreMuRateList(preMuRate);
        ExcelUtil<PreMuRate> util = new ExcelUtil<PreMuRate>(PreMuRate.class);
        util.exportExcel(response, list, "评级管理数据");
    }

    /**
     * 获取评级管理详细信息
     */
    @RequiresPermissions("premu:rate:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuRateService.selectPreMuRateByBaseId(baseId));
    }

    /**
     * 新增评级管理
     */
    @RequiresPermissions("premu:rate:add")
    @Log(title = "评级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuRate preMuRate)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        boolean flag= preMuRateService.getByDistrictCode(district,preMuRate.getYears());
        Integer iTotle= preMuRate.getRateA()+ preMuRate.getRateB()+ preMuRate.getRateC()+ preMuRate.getRateD();
        double rateA=Double.valueOf(preMuRate.getRateA());
        double rateD=Double.valueOf(preMuRate.getRateD());
        double totle=Double.valueOf(iTotle);
        if(flag){
         return warn("该年度已做评级！");

        }else{
            if(iTotle!=100){
                return warn("比例之和应为100！");
            }else if(rateA/totle>0.2){
                return warn("A类比例不大于20%！");
            }else {

                preMuRate.setDistrictCode(district);
                return toAjax(preMuRateService.insertPreMuRate(preMuRate));
            }
        }
    }

    /**
     * 修改评级管理
     */
    @RequiresPermissions("premu:rate:edit")
    @Log(title = "评级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuRate preMuRate)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        Integer iTotle= preMuRate.getRateA()+ preMuRate.getRateB()+ preMuRate.getRateC()+ preMuRate.getRateD();
        double rateA=Double.valueOf(preMuRate.getRateA());
        double rateD=Double.valueOf(preMuRate.getRateD());
        double totle=Double.valueOf(iTotle);

            if(iTotle!=100){
                return warn("比例之和应为100！");
            }else if(rateA/totle>0.2){
                return warn("A类比例不大于20%！");
            }else {
                preMuRate.setDistrictCode(district);
                return toAjax(preMuRateService.updatePreMuRate(preMuRate));
            }
    }

    /**
     * 删除评级管理
     */
    @RequiresPermissions("premu:rate:remove")
    @Log(title = "评级管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuRateService.deletePreMuRateByBaseIds(baseIds));
    }

}
